package com.wc.wcWebsite_core;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.imageio.ImageIO;

import org.apache.tomcat.util.http.fileupload.ByteArrayOutputStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.wc.wcWebsite_api.CommomService;

@Service("commomService")
public class CommomServiceImpl implements CommomService{
	@Autowired
	private DefaultKaptcha defaultKaptcha;

	@Override
	public Map<String,Object> getRandCode() {
		
		Map<String,Object> map=new HashMap<String,Object>();
		
		String randCode=defaultKaptcha.createText();
		BufferedImage image=defaultKaptcha.createImage(randCode);
		
		ByteArrayOutputStream temp=new ByteArrayOutputStream();
		try {
			ImageIO.write(image, "jpg", temp);
			byte[] imgByteArray=temp.toByteArray();
			map.put("imgByteArray", imgByteArray);
			temp.flush();
			temp.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		map.put("randCode", randCode);
		
		return map;
		
	}

}
